package tests;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import simulation.AgentManager;
import simulation.TrafficInfrastructureManager;
import agents.resource.CrossRoadAgent;
import agents.resource.ResourceAgent;
import agents.resource.RoadAgent;
import domain.Car;
import domain.CrossRoad;
import domain.Road;
import domain.Segment;

public class ExperimentSetup1 implements ExperimentSetup {
	
	public ExperimentSetup1() {
		infrastructure = new TrafficInfrastructureManager();
		agents = new AgentManager();
		createTest();
	}
	
	private void createTest() {
		
		CrossRoad c1 = new CrossRoad(4,7,"c1");
		infrastructure.addCrossRoad(c1);
		CrossRoadAgent agentC1 = new CrossRoadAgent(c1);
		agents.addResourceAgent(agentC1); 
		//System.out.println( "X = " + agentC1.getCrossRoad().getSegment().getX() + "Y = " +agentC1.getCrossRoad().getSegment().getY());
		
		CrossRoad c2 = new CrossRoad(1,1,"c2");
		infrastructure.addCrossRoad(c2);
		CrossRoadAgent agentC2 = new CrossRoadAgent(c2);
		agents.addResourceAgent(agentC2);
		//System.out.println("X = " + agentC2.getCrossRoad().getSegment().getX() + "Y = " +agentC2.getCrossRoad().getSegment().getY());
		
		Road road21 = new Road(c2,c1,"road21");
		infrastructure.addRoad(road21);
		RoadAgent agent12 = new RoadAgent(road21, agentC2, agentC1);
		agents.addResourceAgent(agent12);
		System.out.println("segments");
		for(Segment s : agent12.getTrafficInfrastructure().getSegments()){
			System.out.println(	"X = " + s.getX() + " Y = " + s.getY()	);
		}
		
		CrossRoad c3 = new CrossRoad(0,10,"c3");
		infrastructure.addCrossRoad(c3);
		CrossRoadAgent agentC3 = new CrossRoadAgent(c3);
		agents.addResourceAgent(agentC3);
		//System.out.println("X = " + agentC3.getCrossRoad().getSegment().getX() + "Y = " +agentC3.getCrossRoad().getSegment().getY());
		
		Road road13 = new Road(c1,c3,"road13");
		infrastructure.addRoad(road13);
		RoadAgent agent13 = new RoadAgent(road13, agentC1, agentC3);
		agents.addResourceAgent(agent13);
		//System.out.println("segments");
		//for(Segment s : agent13.getRoad().getSegments()){
			//System.out.println(	"X = " + s.getX() + " Y = " + s.getY()	);
		//}
		
		CrossRoad c4 = new CrossRoad(4,8,"c4");
		infrastructure.addCrossRoad(c4);
		
		ArrayList<ResourceAgent> inAndOut = new ArrayList<ResourceAgent>();
		inAndOut.add(agentC1);
		
		CrossRoadAgent agentC4 = new CrossRoadAgent(c4, inAndOut, inAndOut);
		
		agents.addResourceAgent(agentC4);
			
		CrossRoad c5 = new CrossRoad(6,10,"c5");
		infrastructure.addCrossRoad(c5);
		CrossRoadAgent agentC5 = new CrossRoadAgent(c5);
		agents.addResourceAgent(agentC5);
		
		Road road54 = new Road(c5,c4,"road54");
		infrastructure.addRoad(road54);
		RoadAgent agent54 = new RoadAgent(road54, agentC5, agentC4);
		agents.addResourceAgent(agent54);
		
		Road road35 = new Road(c3,c5, "road35");
		infrastructure.addRoad(road35);
		RoadAgent agent35 = new RoadAgent(road35, agentC3, agentC5);
		agents.addResourceAgent(agent35);
		
		CrossRoad c6 = new CrossRoad(12,6, "c6");
		infrastructure.addCrossRoad(c6);
		CrossRoadAgent agentC6 = new CrossRoadAgent(c6);
		agents.addResourceAgent(agentC6);
		
		Road road46 = new Road(c4,c6, "road46");
		infrastructure.addRoad(road46);
		RoadAgent agent46 = new RoadAgent(road46, agentC4, agentC6);
		agents.addResourceAgent(agent46);
		
		CrossRoad c7 = new CrossRoad(8 , 0, "c7");
		infrastructure.addCrossRoad(c7);
		CrossRoadAgent agentC7 = new CrossRoadAgent(c7);
		agents.addResourceAgent(agentC7);
		
		Road road67 = new Road(c6,c7,"road67");
		infrastructure.addRoad(road67);
		RoadAgent agent67 = new RoadAgent(road67, agentC6, agentC7);
		agents.addResourceAgent(agent67);
		
		Road road72 = new Road(c7,c2, "road72");
		infrastructure.addRoad(road72);
		RoadAgent agent72 = new RoadAgent(road72, agentC7, agentC2);
		agents.addResourceAgent(agent72);
		
		CrossRoad c8 = new CrossRoad(6, 3, "c8");
		infrastructure.addCrossRoad(c8);
		CrossRoadAgent agentC8 = new CrossRoadAgent(c8);
		agents.addResourceAgent(agentC8);
		
		Road road78 = new Road(c7,c8,"road78");
		infrastructure.addRoad(road78);
		RoadAgent agent78 = new RoadAgent(road78, agentC7, agentC8);
		agents.addResourceAgent(agent78);
		
		Road road82= new Road(c8,c2,"road82");
		infrastructure.addRoad(road82);
		RoadAgent agent82 = new RoadAgent(road82, agentC8, agentC2);
		agents.addResourceAgent(agent82);
		
		Road road86= new Road(c8,c6,"road86");
		infrastructure.addRoad(road86);
		RoadAgent agent86 = new RoadAgent(road86, agentC8, agentC6);
		agents.addResourceAgent(agent86);
		
		Road road81= new Road(c8,c1,"road81");
		infrastructure.addRoad(road81);
		RoadAgent agent81 = new RoadAgent(road81, agentC8, agentC1);
		agents.addResourceAgent(agent81);
			
		Collection<Car> allCars = new ArrayList<Car>();
		
		allCars.add(new Car(c1.getSegment(), "porshe"));
		allCars.add(new Car( road21.getSegments().get(0), "volvo" ));
		allCars.add(new Car( road67.getSegments().get(0), "audi" ));
		allCars.add(new Car( road21.getSegments().get(2), "pk" ));
		allCars.add(new Car( road21.getSegments().get(3), "renault" ));
		allCars.add(new Car( road21.getSegments().get(1), "vw" ));
		allCars.add(new Car( road86.getSegments().get(0), "ferrari" ));
		allCars.add(new Car( road78.getSegments().get(0), "suzuki" ));
		allCars.add(new Car( road72.getSegments().get(6), "opel" ));
		allCars.add(new Car( road82.getSegments().get(4), "ford" ));
		allCars.add(new Car( road82.getSegments().get(3), "spyker" ));
		allCars.add(new Car( road67.getSegments().get(1), "mclaren" ));
		allCars.add(new Car( c8.getSegment(), "toyota" ));
		
		for(Car c : allCars) {
			infrastructure.addCar(c);
			beginCarPositions.put(c, c.getPosition());
		}
	}

	public TrafficInfrastructureManager getInfrastructure(){
		return infrastructure;
	}
	
	public AgentManager getAgents(){
		return agents;
	}
	
	private TrafficInfrastructureManager infrastructure;
	private AgentManager agents;
	
	public Segment getStartPosition(Car car){
		return beginCarPositions.get(car);
	}
	
	private Map<Car, Segment> beginCarPositions = new HashMap<Car, Segment>();
}
